<?php

$rootdir = '../';
include_once('session.php');

global $desql;
$desql->Connect();


function CreateTshirt($Name, $Reference, $Genre,
					  $Dispo_xs, $Dispo_s, $Dispo_m, $Dispo_l, $Dispo_xl,
					  $Prix)
{
	global $desql;
	
	$result = $desql->Query('INSERT INTO tshirts(nom,
												reference,
												genre,
												dispo_xs,
												dispo_s,
												dispo_m,
												dispo_l,
												dispo_xl,
												prix) 
										VALUES(\'' . mysql_real_escape_string($Name) . '\',
												\'' . mysql_real_escape_string($Reference) . '\',
												\'' . mysql_real_escape_string($Genre) . '\',
												\'' . mysql_real_escape_string($Dispo_xs) . '\',
												\'' . mysql_real_escape_string($Dispo_s) . '\',
												\'' . mysql_real_escape_string($Dispo_m) . '\',
												\'' . mysql_real_escape_string($Dispo_l) . '\',
												\'' . mysql_real_escape_string($Dispo_xl) . '\',
												\'' . mysql_real_escape_string($Prix) . '\'
												)');
												
	$id = mysql_insert_id();
	if($result == false)
		return false;
	else if(TraiterFILECreateTshirtForm($id) == false)
	{
		// Supprimer le Tshirt
		return false;
	}
	else
		return true;
}

function GetDispoByIDAndSize($ID, $Taille)
{
	$tshirt = GetTshirtByID($ID);
	if($tshirt == false)
		return false;
	else
		return $tshirt['dispo_' . $Taille];
}

function GetIsDispo($ID)
{
	$tshirt = GetTshirtByID($ID);
	if($tshirt == false)
		return false;
	else
		return $tshirt['dispo_xs'] || $tshirt['dispo_s'] || $tshirt['dispo_m'] || $tshirt['dispo_l'] || $tshirt['dispo_xl'];
}

function UpdateTshirt($tshirt)
{
	global $desql;
	return $desql->Query('UPDATE tshirts SET nom=\'' . mysql_real_escape_string($tshirt['nom']) . '\',
										reference=\'' . mysql_real_escape_string($tshirt['reference']) . '\',
										genre=\'' . mysql_real_escape_string($tshirt['genre']) . '\', 
										dispo_xs=\'' . mysql_real_escape_string($tshirt['dispo_xs']) . '\', 
										dispo_s=\'' . mysql_real_escape_string($tshirt['dispo_s']) . '\', 
										dispo_m=\'' . mysql_real_escape_string($tshirt['dispo_m']) . '\', 
										dispo_l=\'' . mysql_real_escape_string($tshirt['dispo_l']) . '\', 
										dispo_xl=\'' . mysql_real_escape_string($tshirt['dispo_xl']) . '\', 
										prix=\'' . mysql_real_escape_string($tshirt['prix']) . '\'
									WHERE id=' . mysql_real_escape_string($tshirt['id']));

}

function DeleteTshirtByID($id)
{
	global $desql;
	
	if(is_numeric($id) == false)
		return false;
	else
	{
		unlink('tshirts/' . $id . '_infotshirt_1_petite.png');
		unlink('tshirts/' . $id . '_infotshirt_1_moyenne.png');
		unlink('tshirts/' . $id . '_infotshirt_1_grande.png');
		unlink('tshirts/' . $id . '_infotshirt_2_petite.png');
		unlink('tshirts/' . $id . '_infotshirt_2_moyenne.png');
		unlink('tshirts/' . $id . '_infotshirt_2_grande.png');
		unlink('tshirts/' . $id . '_infotshirt_3_petite.png');
		unlink('tshirts/' . $id . '_infotshirt_3_moyenne.png');
		unlink('tshirts/' . $id . '_infotshirt_3_grande.png');
		unlink('tshirts/' . $id . '_galery.png');
		unlink('tshirts/' . $id . '_explication.png');
		
		return $desql->Query('DELETE FROM tshirts WHERE id=' . $id);
	}
}

function GetTshirtByID($ID)
{
	global $desql;
	
	if(is_numeric($ID) == false)
		return false;
	else
		$result = $desql->Query('SELECT * FROM tshirts WHERE id=' . $ID);
	
	if($result == false)
		return false;
		
	if($t = mysql_fetch_array($result))
		return $t;
	else
		return false;
}

function GetAllTshirts()
{
	global $desql;
	$tshirts = array();
	
	$answer = $desql->Query('SELECT * FROM tshirts');
	if($answer == false)
		return false;
		
	while($tshirt = mysql_fetch_array($answer))
		$tshirts[$tshirt['id']] = $tshirt;
	
	return $tshirts;
}

function GetAllTshirtsByGenre($Genre)
{
	global $desql;
	$tshirts = array();
	
	$answer = $desql->Query('SELECT * FROM tshirts WHERE genre=\'' . mysql_real_escape_string($Genre) . '\'');
	if($answer == false)
		return false;
		
	while($tshirt = mysql_fetch_array($answer))
		$tshirts[$tshirt['id']] = $tshirt;
	
	return $tshirts;
}

function GetModifyTshirtForm($ID)
{
	$tshirt = GetTshirtById($ID);
		
	if($tshirt == false)
		return '<p style="color: red; margin-left: 40px;">Tshirt non trouvé ou erreur de communication avec la bae de données.</p>';
	
	$HTML = '<h4>Modifier Tshirt</h4>
			<form method="post">
			<h5>Infos générales</h5>
				<table style="margin: auto;">
					<tr>
						<td><label for="nom">Nom :</label></td>
						<td><input type="text" tabindex="1" name="nom" size="50" value="' . $tshirt['nom'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="reference">Référence :</label></td>
						<td><input type="text" tabindex="2" name="reference" size="50" value="' . $tshirt['reference'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="genre">Genre :</label></td>
						<td><select tabindex="3" name="genre" id="genre"> ';
	if($tshirt['genre'] == 'homme')
		$HTML .= '	<option selected value="homme">Homme</option>
					<option value="femme">Femme</option>';
	else
		$HTML .= '	<option value="homme">Homme</option>
					<option selected value="femme">Femme</option>';
	$HTML .= '				</select></td>
					</tr>
					<tr>
						<td><label for="prix">Prix :</label></td>
						<td><input type="text" tabindex="4" name="prix" size="6" value="' . $tshirt['prix'] . '" /><br /></td>
					</tr>
				</table>
				<br /><br /><br />
				<h5>Quantités</h5>

				<table style="margin: auto;">
					<tr>
						<td><label for="xs">Dispo XS :</label></td>
						<td><input type="text" tabindex="5" name="xs" size="6" value="' . $tshirt['dispo_xs'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="s">Dispo S :</label></td>
						<td><input type="text" tabindex="6" name="s" size="6" value="' . $tshirt['dispo_s'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="m">Dispo M :</label></td>
						<td><input type="text" tabindex="7" name="m" size="6" value="' . $tshirt['dispo_m'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="l">Dispo L :</label></td>
						<td><input type="text" tabindex="8" name="l" size="6" value="' . $tshirt['dispo_l'] . '" /><br /></td>
					</tr>
					<tr>
						<td><label for="xl">Dispo XL :</label></td>
						<td><input type="text" tabindex="9" name="xl" size="6" value="' . $tshirt['dispo_xl'] . '" /><br /></td>
					</tr>
				</table>
				<br /><br /><br /><div style="text-align: center;"><input type="submit" name="sendmodif" tabindex="10" value="Envoyer" /></div>
				<input type="hidden"  name="tshirtid"  value="' . $tshirt['id'] . '">
			</form>
			';
			
	return $HTML;
}

function GetCreateTshirtForm()
{
	$HTML = '<h4>Nouveau Tshirt</h4>
			<form method="post" enctype="multipart/form-data">
				<table style="margin: auto;">
					<tr>
						<td><label for="nom">Nom :</label></td>
						<td><input type="text" tabindex="1" name="name" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="reference">Référence :</label></td>
						<td><input type="text" tabindex="2" name="reference" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="genre">Genre :</label></td>
						<td>
							<select tabindex="3" name="genre" id="genre">
								<option value="homme">Homme</option>
								<option value="femme">Femme</option>
							</select>
						</td>	
					</tr>
					<tr>
						<td><label for="image-galerie">Image galerie (png):</label></td>
						<td><input type="file" tabindex="4" name="image-galerie" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="image-explication">Image explication du tshirt (png):</label></td>
						<td><input type="file" tabindex="5" name="image-explication" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="image-infotshirt-1">Imagette 1 (png):</label></td>
						<td><input type="file" tabindex="6" name="image-infotshirt-1" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="image-infotshirt-2">Imagette 2 (png):</label></td>
						<td><input type="file" tabindex="7" name="image-infotshirt-2" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="image-infotshirt-3">Imagette 3 (png):</label></td>
						<td><input type="file" tabindex="8" name="image-infotshirt-3" size="30" /><br /></td>
					</tr>
					<tr>
						<td><label for="prix">Prix :</label></td>
						<td><input type="text" tabindex="9" name="prix" size="6" value="20"/><br /></td>
					</tr>
					<tr>
						<td><label for="xs">Dispo XS :</label></td>
						<td><input type="text" tabindex="10" name="xs" size="6" /><br /></td>
					</tr>
					<tr>
						<td><label for="s">Dispo S :</label></td>
						<td><input type="text" tabindex="11" name="s" size="6" /><br /></td>
					</tr>
					<tr>
						<td><label for="m">Dispo M :</label></td>
						<td><input type="text" tabindex="12" name="m" size="6" /><br /></td>
					</tr>
					<tr>
						<td><label for="l">Dispo L :</label></td>
						<td><input type="text" tabindex="13" name="l" size="6" /><br /></td>
					</tr>
					<tr>
						<td><label for="xl">Dispo XL :</label></td>
						<td><input type="text" tabindex="14" name="xl" size="6" /><br /></td>
					</tr>
					<tr>
						<td></td>
						<td><br /><input type="submit" name="send" tabindex="15" value="Envoyer" /></td>
					</tr>
				</table>
			</form>';
			
	return $HTML;
}

function TraiterFILECreateTshirtForm($id)
{
	$image_galerie = imagecreatefrompng($_FILES['image-galerie']['tmp_name']);
	$image_infotshirt_1 = imagecreatefrompng($_FILES['image-infotshirt-1']['tmp_name']);
	$image_infotshirt_2 = imagecreatefrompng($_FILES['image-infotshirt-2']['tmp_name']);
	$image_infotshirt_3 = imagecreatefrompng($_FILES['image-infotshirt-3']['tmp_name']);
	$image_galerie_real = imagecreatetruecolor(233, 381);
	$image_infotshirt_1_petite_real = imagecreatetruecolor(80, 112);
	$image_infotshirt_1_moyenne_real = imagecreatetruecolor(300, 419);
	$image_infotshirt_1_grande_real = imagecreatetruecolor(500, 698);
	$image_infotshirt_2_petite_real = imagecreatetruecolor(80, 112);
	$image_infotshirt_2_moyenne_real = imagecreatetruecolor(300, 419);
	$image_infotshirt_2_grande_real = imagecreatetruecolor(500, 698);
	$image_infotshirt_3_petite_real = imagecreatetruecolor(80, 112);
	$image_infotshirt_3_moyenne_real = imagecreatetruecolor(300, 419);
	$image_infotshirt_3_grande_real = imagecreatetruecolor(500, 698);

	
	imagecopyresampled($image_galerie_real, $image_galerie, 0, 0, 0, 0, 233, 381, imagesx($image_galerie), imagesy($image_galerie));
	imagecopyresampled($image_infotshirt_1_petite_real, $image_infotshirt_1, 0, 0, 0, 0, 80, 112, imagesx($image_infotshirt_1), imagesy($image_infotshirt_1));
	imagecopyresampled($image_infotshirt_1_moyenne_real, $image_infotshirt_1, 0, 0, 0, 0, 300, 419, imagesx($image_infotshirt_1), imagesy($image_infotshirt_1));
	imagecopyresampled($image_infotshirt_1_grande_real, $image_infotshirt_1, 0, 0, 0, 0, 500, 698, imagesx($image_infotshirt_1), imagesy($image_infotshirt_1));
	imagecopyresampled($image_infotshirt_2_petite_real, $image_infotshirt_2, 0, 0, 0, 0, 80, 112, imagesx($image_infotshirt_2), imagesy($image_infotshirt_2));
	imagecopyresampled($image_infotshirt_2_moyenne_real, $image_infotshirt_2, 0, 0, 0, 0, 300, 419, imagesx($image_infotshirt_2), imagesy($image_infotshirt_2));
	imagecopyresampled($image_infotshirt_2_grande_real, $image_infotshirt_2, 0, 0, 0, 0, 500, 698, imagesx($image_infotshirt_2), imagesy($image_infotshirt_2));
	imagecopyresampled($image_infotshirt_3_petite_real, $image_infotshirt_3, 0, 0, 0, 0, 80, 112, imagesx($image_infotshirt_3), imagesy($image_infotshirt_3));
	imagecopyresampled($image_infotshirt_3_moyenne_real, $image_infotshirt_3, 0, 0, 0, 0, 300, 419, imagesx($image_infotshirt_3), imagesy($image_infotshirt_3));
	imagecopyresampled($image_infotshirt_3_grande_real, $image_infotshirt_3, 0, 0, 0, 0, 500, 698, imagesx($image_infotshirt_3), imagesy($image_infotshirt_3));
	
	imagepng($image_galerie_real, 'tshirts/' . $id . '_galery.png');
	imagepng($image_infotshirt_1_petite_real, 'tshirts/' . $id . '_infotshirt_1_petite.png');
	imagepng($image_infotshirt_1_moyenne_real, 'tshirts/' . $id . '_infotshirt_1_moyenne.png');
	imagepng($image_infotshirt_1_grande_real, 'tshirts/' . $id . '_infotshirt_1_grande.png');
	imagepng($image_infotshirt_2_petite_real, 'tshirts/' . $id . '_infotshirt_2_petite.png');
	imagepng($image_infotshirt_2_moyenne_real, 'tshirts/' . $id . '_infotshirt_2_moyenne.png');
	imagepng($image_infotshirt_2_grande_real, 'tshirts/' . $id . '_infotshirt_2_grande.png');
	imagepng($image_infotshirt_3_petite_real, 'tshirts/' . $id . '_infotshirt_3_petite.png');
	imagepng($image_infotshirt_3_moyenne_real, 'tshirts/' . $id . '_infotshirt_3_moyenne.png');
	imagepng($image_infotshirt_3_grande_real, 'tshirts/' . $id . '_infotshirt_3_grande.png');
	
	move_uploaded_file($_FILES['image-explication']['tmp_name'], 'tshirts/' . $id . '_explication.png');
	
	return true;
}

function GetAdminTshirtsHTMLContent()
{
	$HTML = '';

    $tshirts = GetAllTshirts();

	if($tshirts == false)
		$HTML .= '<p style="color: orange;">Une erreur est survenue ou il n\'y a pas de Tshirts.</p>';
	
	
	$HTML .= '<table id="table_admin_tshirts">
				<tr>
					<th style="width: 30px;">N°</th>
					<th style="width: 200px;">Aperçu</th>
					<th>Nom<br />Référence<br />Genre</th>
					<th style="width: 160px;">Dispo</th>
					<th style="width: 70px;">Prix</th>
					<th style="width: 70px;">Options</th>
				</tr>
				<tr>
					<td></td><td></td><td></td><td></td><td></td>
					<td><div id="icone-nouveau"><a href="cadre_admin.php?type=1&new=1"></a></div></td>
				</tr>
			';
	
	
	foreach($tshirts as $tshirt)
	{
		$HTML .= GetAdminTshirtHTMLContent($tshirt);
	}
	
	$HTML .= '</table>';
	
	return $HTML;
}

function GetAdminTshirtHTMLContent($tshirt)
{
	$HTML = '<tr>';

	$HTML .= '<td>' . $tshirt['id'] . '</td>';
	$HTML .= '<td><img src="./tshirts/' . $tshirt['id'] .'_galery.png"></img></td>';
	$HTML .= '<td>' . $tshirt['nom'] . '<br />' . $tshirt['reference'] . '<br />' . $tshirt['genre'] . '</td>';
	$HTML .= '<td>
				<table style="margin:auto;">
					<tr>	
						<td style="width: 20px">XS</td>
						<td style="width: 31px">' . $tshirt['dispo_xs'] . '</td>
					</tr>
					<tr>
						<td>S</td>
						<td>' . $tshirt['dispo_s'] . '</td>
					</tr>
					<tr>
						<td>M</td>
						<td>' . $tshirt['dispo_m'] . '</td>
					</tr>
					<tr>
						<td>L</td>
						<td>' . $tshirt['dispo_l'] . '</td>					
					</tr>
					<tr>
						<td>XL</td>
						<td>' . $tshirt['dispo_xl'] . '</td>					
					</tr>
				</table>
			 </td>';
	$HTML .= '<td>' . $tshirt['prix'] . ' €</td>';
	$HTML .= '<td>
				<div id="icone-modifier">
					<a href="cadre_admin.php?type=1&modifid=' . $tshirt['id'] . '"></a>
				</div>
				<div id="icone-supprimer">
					<a href="cadre_admin.php?type=1&deleteid=' . $tshirt['id'] . '" onclick="return(confirm(\'Etes-vous sûr de vouloir supprimer ce Tshirt?\'));"></a>
				</div>
			 </td>';
	
	$HTML .= '</tr>';
	return $HTML;
}

function GetGaleryTshirtsHTMLContent($Genre)
{
	$i = 1;
	$HTML = '<table style="border-collapse: collapse;">';
	$HTML .= '<tr>';

    $tshirts = GetAllTshirtsByGenre($Genre);

	if($tshirts == false)
		return '<p style="color: orange;">Une erreur est survenue ou il n\'y a pas de Tshirts.</p>';
		
	foreach($tshirts as $tshirt)
	{
		if($i == 4)
		{
			$HTML .= '</tr>';
			$HTML .= '<tr>';
			$HTML .= '<td style="max-width: 231px; vertical-align: top;">' . GetGaleryTshirtHTMLContent($tshirt) . '</td>';
			$i = 1;
		}
		else
		{
			$HTML .= '<td style="max-width: 231px; vertical-align: top;">' . GetGaleryTshirtHTMLContent($tshirt) . '</td>';
		}
		
		$i++;
	}
	$HTML .= '</tr>';
	$HTML .= '</table>';
	
	return $HTML;
}

function GetGaleryTshirtHTMLContent($tshirt)
{
	$HTML = 
	'<div id="galery-tshirt-container">
		<a href="cadre_infotshirt.php?tshirtid=' . $tshirt['id'] . '" target="iframe">
			<img border="0" src="tshirts/' . $tshirt['id'] . '_galery.png"/>
		</a>
	</div>';
	
	return $HTML;
}



?>